package mpicbg.models;

import java.util.ArrayList;

// sort an arrayList of arrays based on the ith column
public class c
{
private static ArrayList<Float[]> mergeSort(ArrayList<Float[]> arr, int index)
{
     ArrayList<Float[]> left = new ArrayList<Float[]>();
     ArrayList<Float[]> right = new ArrayList<Float[]>();
     if(arr.size()<=1)
         return arr;
     else
     {
         int middle = arr.size()/2;
         for (int i = 0; i<middle; i++)
             left.add(arr.get(i));
         for (int j = middle; j<arr.size(); j++)
             right.add(arr.get(j));
         left = mergeSort(left, index);
         right = mergeSort(right, index);
         return merge(left, right, index);
         
     }
     
}

// merge the the results for mergeSort back together 
private static ArrayList<Float[]> merge(ArrayList<Float[]> left, ArrayList<Float[]> right, int index)
{
     ArrayList<Float[]> result = new ArrayList<Float[]>();
     while (left.size() > 0 && right.size() > 0)
     {
         if(left.get(0)[index] <= right.get(0)[index])
         {
             result.add(left.get(0));
             left.remove(0);
         }
         else
         {
             result.add(right.get(0));
             right.remove(0);
         }
     }
     if (left.size()>0) 
     {
         for(int i=0; i<left.size(); i++)
             result.add(left.get(i));
     }
     if (right.size()>0) 
     {
         for(int i=0; i<right.size(); i++)
             result.add(right.get(i));
     }
     return result;
}
}